package com.bush.mall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.coupon.mapper.CouponSpuCategoryRelationMapper;
import com.bush.mall.coupon.entity.CouponSpuCategoryRelationEntity;
import com.bush.mall.coupon.service.CouponSpuCategoryRelationService;

/**
 * 优惠券分类关联
 * @author RUN
 * @date 2022-11-29
 */
@Service(CouponSpuCategoryRelationService.BEAN_ID)
@Slf4j
public class CouponSpuCategoryRelationServiceImpl extends ServiceImpl<CouponSpuCategoryRelationMapper, CouponSpuCategoryRelationEntity> implements CouponSpuCategoryRelationService {

    /**
     * 查询优惠券分类关联列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<CouponSpuCategoryRelationEntity> page = new Page<>(current,size);
            QueryWrapper<CouponSpuCategoryRelationEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("CouponSpuCategoryRelationService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建优惠券分类关联表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveCouponSpuCategoryRelation(CouponSpuCategoryRelationEntity couponSpuCategoryRelation) {
        try{
            Assert.notNull(couponSpuCategoryRelation,"参数异常");
            boolean flag = this.save(couponSpuCategoryRelation);
            if (flag) return Result.ok("优惠券分类关联添加成功");
            throw new SaaSException("优惠券分类关联添加失败");
        }catch(Exception e){
            log.error("CouponSpuCategoryRelationService saveCouponSpuCategoryRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改优惠券分类关联表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateCouponSpuCategoryRelation(CouponSpuCategoryRelationEntity couponSpuCategoryRelation) {
        try{
            Assert.notNull(couponSpuCategoryRelation,"参数异常");
            boolean flag = this.updateById(couponSpuCategoryRelation);
            if (flag) return Result.ok("优惠券分类关联修改成功");
            throw new SaaSException("优惠券分类关联修改失败");
        }catch(Exception e){
            log.error("CouponSpuCategoryRelationService updateCouponSpuCategoryRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除优惠券分类关联表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteCouponSpuCategoryRelation(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("优惠券分类关联删除成功");
            throw new SaaSException("优惠券分类关联删除失败");
        }catch(Exception e){
            log.error("CouponSpuCategoryRelationService deleteCouponSpuCategoryRelation ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}